/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mp.health.manager.intercept;

import com.mp.health.common.Constants;
import com.mp.health.form.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Map;
import org.apache.struts2.dispatcher.RequestMap;

/**
 *
 * @author vannh
 * Thuc hien kiem tra thong tin session moi khi request len server
 */
public class AuthenticateSessionInterceptor implements Interceptor{

    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    @Override
    /*
     * Thuc hien nghiem vu kiem tra
     */
    public String intercept(ActionInvocation ai) throws Exception {
        Map sessionMap = ai.getInvocationContext().getSession();
        User user = (User)sessionMap.get(Constants.USER_INFOR_SESSION);
        RequestMap rs = (RequestMap)ai.getInvocationContext().get("request");
        if(user != null && user.getIsValid()){
            return ai.invoke();
        } else {
            return Action.LOGIN;
        }
    }
}
